/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucuenca.edu.optii.ecu911.gui;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import ucuenca.edu.optii.ecu911.negocio.EntidadBomberos;
import ucuenca.edu.optii.ecu911.negocio.EntidadCooperativa;
import ucuenca.edu.optii.ecu911.negocio.Operador_EntidadC;
import ucuenca.edu.optii.ecu911.negocio.Telefono;
import ucuenca.edu.optii.ecu911.negocio.VectoresObj;
import ucuenca.edu.optii.ecu911.negocio.Visitor;
import ucuenca.edu.optii.ecu911.negocio.excepciones.TelefonoVerificacionException;
import ucuenca.edu.optii.ecu911.negocio.excepciones.ValidacionCamposTextoExcepcion;
import ucuenca.edu.optii.ecu911.negocio.excepciones.ValidacionGrabacionDatos;

/**
 *
 * @author May
 */
public class GestionOperadoresEntidad extends javax.swing.JPanel {
Operador_EntidadC operadorEntidad=new Operador_EntidadC();
EntidadCooperativa mientidad=new EntidadCooperativa() {

    @Override
    public String aceptar(Visitor visitor) {
        return "";
    }
};
VectoresObj unvector=new VectoresObj();
    /**
     * Creates new form GestionOperadoresEntidad
     */
    public GestionOperadoresEntidad() {
        initComponents();
        actualizaOperadoresEntidad();
        actualizaEntidadesC();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        t_OperadorCooperativa = new javax.swing.JTable();
        txtcedOpCoop = new javax.swing.JTextField();
        txtcorreo = new javax.swing.JTextField();
        txtsueldoU = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        addUser = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        ponEntidadId = new javax.swing.JLabel();
        comboEntidades = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();

        jLabel1.setText("Cedula");

        jLabel2.setText("correo");

        jLabel4.setText("Sueldo");

        t_OperadorCooperativa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "cedula", "correo", "sueldo", "entidad coop", "telefono"
            }
        ));
        jScrollPane1.setViewportView(t_OperadorCooperativa);

        txtcedOpCoop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcedOpCoopActionPerformed(evt);
            }
        });

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        addUser.setText("registrar");
        addUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addUserActionPerformed(evt);
            }
        });

        jButton2.setText("modificar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("eliminar");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(addUser, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addUser)
                .addGap(8, 8, 8)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel14.setText("Entidad");

        ponEntidadId.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        comboEntidades.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboEntidades.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                comboEntidadesMouseClicked(evt);
            }
        });

        jLabel3.setText("Personal Contratado en Entidades Cooperativas");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(33, 33, 33)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtcedOpCoop, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                                    .addComponent(txtcorreo))
                                .addGap(29, 29, 29)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel14))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(48, 48, 48)
                                        .addComponent(txtsueldoU, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(47, 47, 47))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ponEntidadId, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(comboEntidades, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)))
                                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 524, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(194, 194, 194)
                        .addComponent(jLabel3)))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtcedOpCoop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(txtsueldoU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ponEntidadId, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comboEntidades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtcorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2)
                                .addComponent(jLabel14)))
                        .addGap(68, 68, 68))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
     public void actualizaEntidadesC() {
        List misentidades = new ArrayList();
        unvector.listarEntidadesDisponibles();
        misentidades = unvector.getEntidadesDisponibles();
        comboEntidades.removeAllItems();
        comboEntidades.addItem("");
        for (int i = 0; i < misentidades.size(); i++) {
            EntidadCooperativa otroTipo = new EntidadCooperativa() {

                @Override
                public String aceptar(Visitor visitor) {
                    return "";
                }
            };
            otroTipo = (EntidadCooperativa) misentidades.get(i);
            otroTipo.buscar();
            comboEntidades.addItem(otroTipo.getId() + ",  " + otroTipo.getTipo()+" ,  "+otroTipo.getCiudad());
        }
    }
    public void actualizaOperadoresEntidad() {
        unvector.listarOperadoresEntidades();
        ArrayList operads = new ArrayList();
        operads = unvector.getOpEntidades();
        DefaultTableModel temp = (DefaultTableModel) t_OperadorCooperativa.getModel();
        for (int i = t_OperadorCooperativa.getRowCount() - 1; i >= 0; i--) {
            temp.removeRow(i);
        }
        for (int i = 0; i < operads.size(); i++) {
            Operador_EntidadC otrop = new Operador_EntidadC();
            otrop = (Operador_EntidadC) operads.get(i);
            
            EntidadCooperativa entidad=new EntidadCooperativa() {

                @Override
                public String aceptar(Visitor visitor) {
                    return "";
                }
            };
            entidad.setId(otrop.getMientidad().getId());
            String tipo=entidad.buscarEntidadHijo();
            entidad.buscar();
            
            Telefono mifono=new Telefono();
            mifono.setId(entidad.getMifono().getId());
            try {
                mifono.buscarNumeroSegunIde();
            } catch (TelefonoVerificacionException ex) {
                Logger.getLogger(ex.getMessage());
            }
            
            Object nuevo[] = {otrop.getCedula(), otrop.getCorreo_el(), otrop.getSueldo(), tipo, mifono.getNumero()}; //esto es por las tres columnas aunque puede variar
            temp.addRow(nuevo);
        }
        t_OperadorCooperativa.setModel(temp);
    }
    private void comboEntidadesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_comboEntidadesMouseClicked
        // TODO add your handling code here:
        StringTokenizer tokens = new StringTokenizer((String) comboEntidades.getSelectedItem(), ",");
        try {
            ponEntidadId.setText(tokens.nextToken());
        } catch (NoSuchElementException e) {
        }
    }//GEN-LAST:event_comboEntidadesMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void addUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addUserActionPerformed
        if (txtcedOpCoop.getText().equals("") || txtcorreo.getText().equals("") || txtsueldoU.getText().equals("")||ponEntidadId.getText().equals("")) {
            throw new ValidacionCamposTextoExcepcion( "Debe llenar los campos de texto");
        } else {
            operadorEntidad.setCedula(txtcedOpCoop.getText());
            operadorEntidad.setCorreo_el(txtcorreo.getText());
            operadorEntidad.setSueldo(Double.valueOf(txtsueldoU.getText()));
            mientidad.setId(Integer.valueOf(ponEntidadId.getText()));
            operadorEntidad.setMientidad(mientidad);
            boolean r = operadorEntidad.grabar();
            if (r == true) {
                JOptionPane.showMessageDialog(null, "Datos Grabados Satisfactoriamente...");
            } else if (r == false) {
                throw  new ValidacionGrabacionDatos( "Error al Grabar datos");
            }
            actualizaOperadoresEntidad();
            actualizaEntidadesC();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_addUserActionPerformed

    private void txtcedOpCoopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcedOpCoopActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtcedOpCoopActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addUser;
    private javax.swing.JComboBox comboEntidades;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel ponEntidadId;
    private javax.swing.JTable t_OperadorCooperativa;
    private javax.swing.JTextField txtcedOpCoop;
    private javax.swing.JTextField txtcorreo;
    private javax.swing.JTextField txtsueldoU;
    // End of variables declaration//GEN-END:variables
}
